package com.controller;

import com.api.CommonResult;
import com.entity.table.FUserTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.FUser;
import com.service.FUserService;

import java.io.Serializable;
import java.util.List;

/**
 * 用户表 控制层。
 *
 * @author admin
 * @since 2025-04-21
 */
@Controller
@RequestMapping("/admin/user")
public class FUserController {

    @Autowired
    private FUserService fUserService;

    /**
     * 添加用户表。
     *
     * @param fUser 用户表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
//    @PostMapping("save")
//    public boolean save(@RequestBody FUser fUser) {
//        return fUserService.save(fUser);
//    }

    /**
     * 根据主键删除用户表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return fUserService.removeById(id);
    }

    /**
     * 根据主键更新用户表。
     *
     * @param fUser 用户表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
//    @PutMapping("update")
//    public boolean update(@RequestBody FUser fUser) {
//        return fUserService.updateById(fUser);
//    }

    /**
     * 查询所有用户表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<FUser> list() {
        return fUserService.list();
    }

    /**
     * 根据用户表主键获取详细信息。
     *
     * @param id 用户表主键
     * @return 用户表详情
     */
    @GetMapping("getInfo/{id}")
    public FUser getInfo(@PathVariable Serializable id) {
        return fUserService.getById(id);
    }

    /**
     * 分页查询用户表。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<FUser> page(Page<FUser> page) {
        return fUserService.page(page);
    }

    @GetMapping(value = "/userList")
    public String userList(ModelMap model , @RequestParam(value = "userName",defaultValue = "")String userName,
                           @RequestParam(value = "p",defaultValue = "1")int p,
                           @RequestParam(value = "pageSize", defaultValue = "10")int pageSize){
        QueryWrapper queryWrapper = QueryWrapper.create().where(FUserTableDef.F_USER.USER_NAME.like(userName));
        Page<FUser> page=new Page<>();
        page.setPageSize(pageSize);
        page.setPageNumber(p);
        Page<FUser> pageInfo = fUserService.page(page,queryWrapper);
        model.addAttribute("page",pageInfo);
        model.addAttribute("total",pageInfo.getTotalRow());
        return "back/user-list";
    }

    @GetMapping(value = "/addView")
    public String addUserView(){return "back/user-add";}


    @PostMapping("save")
    @ResponseBody
    public CommonResult save(FUser fUser){
        try{
            boolean r =fUserService.save(fUser);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            String msg=fUser.getUserName()+"已存在";
            return CommonResult.failed(msg);
        }
    }

    @GetMapping(value = "updateView")
    public  String updateView(int id,ModelMap model){
        FUser fUser1 =fUserService.getById(id);
        model.addAttribute("user",fUser1);
        return "back/user-edit";
    }

    @PostMapping("update")
    @ResponseBody
    public CommonResult update(FUser fUser){
        try{
            boolean r =fUserService.updateById(fUser);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            String msg = fUser.getUserName()+"已存在";
            return CommonResult.failed(msg);
        }
    }

    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id){
        try{
        boolean r =fUserService.removeById(id);
        return  CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }
}
